package office.balancedBinaryTree;


//给定一个二叉树，判断它是否是
//平衡二叉树
class TreeNode {
    int val;
    TreeNode right;
    TreeNode left;
    TreeNode (int val, TreeNode right, TreeNode left) {
        this.val = val;
        this.right = right;
        this.left = left;
    }
}
public class main {

    public boolean isBalanced(TreeNode root) {
        if(root == null) {
            return true;
        } else {
            return Math.abs(height(root.left) - height(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right);
        }
    }

    public int height(TreeNode root) {
        if(root == null) {
            return 0;
        } else {
            return Math.max(height(root.left), height(root.right)) + 1;
        }
    }
}
